<script setup>
import {
  Edit, Delete, Position, Upload
} from '@element-plus/icons-vue'
import {

} from '@/api/allApi.js'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getallbookservice, getpartpathservice,
  updatepathservice, detailbookservice, deletepathservice, lendbookservice, getallbookcategoryservice
} from '@/api/book.js'
import { getPayPhasesservice, deletef3service } from '@/api/allApi.js'
import {
  addinputservice, getpartclientservice,getcontractsservice,getPayPhasesforinquiryservice,getresetpricesservice,
  getAllUserForClientComeAddservice, getPayPhaseByContractIdservice,deleteinputservice
} from '@/api/client.js'
//控制添加分类弹窗
const dialogVisible = ref(false)
const lenddialogVisible = ref(false)
//分页条数据模型
//const pageNum = ref(1)//当前页
const total = ref(20)//总条数
//const pageSize = ref(3)//每页条数
//文章分类数据模型
const clients = ref([])
const users = ref([])
const contracts = ref([])
const userdepartment = ref(null)
const payphases = ref([])
//用户搜索时选中的分类id
const bookcategorylist = async () => {
  let result = await getpartclientservice({
    pageNum: 1,
    pageSize: 1000,
    str: ''
  });
  clients.value = result.data.items;
  console.log(clients.value);

}
bookcategorylist();
const listuser = async () => {
  let result = await getAllUserForClientComeAddservice();

  users.value = result.data;
  console.log(result);
}
listuser();
//定义变量，控制标题
const title = ref('')
//与后端交互的json数据
const pathmodel = ref({
  id: null,
  contractid: null,
  inputprice: null,
  paytime: null,
  userid: null,
  comment: null
})
const sheets = ref([{}, {}])
const findmodel = ref({
  pageNum: 1,
  pageSize: 5,
  str: ''
})


const getcontracts = async () => {
  let result = await getcontractsservice({
  pageNum: 1,
  pageSize: 1000,
  str: ''
});
  contracts.value = result.data.items;
};
getcontracts();

const f3list = async () => {
    console.log(findmodel.value);
    let result = await getPayPhasesforinquiryservice(findmodel.value);
    console.log(result.items);
    total.value = result.data.total;
    sheets.value = result.data.items;
}
f3list();
//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  findmodel.value.pageSize = size;
  f3list();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  findmodel.value.pageNum = num;
  f3list();
}
const cleardata = () => {
  findmodel.value.str = ''
}
//表单校验
const rules = {
  name: [
    { required: true, message: '请选择合同', trigger: 'blur' },
  ],
  clientcompanyname: [
    { required: true, message: '请选择合同', trigger: 'blur' },
  ],
  allprices: [
    { required: true, message: '请选择合同', trigger: 'blur' },
  ],
  clientcompanyid: [
    { required: true, message: '请选择合同', trigger: 'blur' },
  ],
  resetprices: [
    { required: true, message: '请选择合同', trigger: 'blur' },
  ],
  contractid: [
    { required: true, message: '请选择合同', trigger: 'blur' },
  ],
  comment: [
    { required: true, message: '请输入备注', trigger: 'blur' },
  ],
  
  paytime: [
    { required: true, message: '请选择到账时间', trigger: 'blur' },
  ],
  inputprice: [
    { required: true, message: '请输入到款金额', trigger: 'blur' },
    {
            validator: (rule, value, callback) => {
                if (value < 0) {
                    callback(new Error('金额不能为负数'));
                } else {
                    callback();
                }
            }, trigger: 'blur'
        }
  ],
}
const formref = ref(null); //表单校验
const addpath = async () => {
  dialogVisible.value = false;
}
//展示编辑弹窗
const showDialog = async (row) => {
  title.value = '合同应收帐详细信息';
  dialogVisible.value = true;
  //数据拷贝
  pathmodel.value.contractid = row.contractid
  pathmodel.value.name = row.contract.name
    pathmodel.value.clientcompanyname = row.contract.quote.chance.clientCompany.clientcompanyname
    pathmodel.value.allprices = row.contract.quote.allprices
    pathmodel.value.clientcompanyid = row.contract.quote.chance.clientCompany.id
    let result = await getresetpricesservice(row.contractid)
  pathmodel.value.resetprices = result.data
  let result2 = await getPayPhaseByContractIdservice(row.contractid);
  payphases.value = result2.data;
  console.log(payphases.value);
  
}

const handleContractChange =async (id) => {
  console.log(id);
  const contract = contracts.value.find(contract => contract.id === id) ?
    contracts.value.find(contract => contract.id === id) : null;
  if (contract != null) {
    pathmodel.value.name = contract.name
    pathmodel.value.clientcompanyname = contract.quote.chance.clientCompany.clientcompanyname
    pathmodel.value.allprices = contract.quote.allprices
    pathmodel.value.clientcompanyid = contract.quote.chance.clientCompany.id
    let result = await getresetpricesservice(id)
    pathmodel.value.resetprices = result.data
  } else {
    pathmodel.value.name = null
    pathmodel.value.clientcompanyname = null
    pathmodel.value.allprices = null
    pathmodel.value.clientcompanyid = null
    pathmodel.value.resetprices = null
  }
}
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>应收账查询</span>
        <!-- <div class="extra">
          <el-button type="primary" @click="add">新增录入</el-button>
        </div> -->
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="查询条件：">
        <el-input v-model="findmodel.str" placeholder="请输入查询内容" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="f3list">搜索</el-button>
        <el-button @click="cleardata">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="sheets" style="width: 100%">
      <el-table-column label="序号" type="index" width="60"> </el-table-column>
      <el-table-column label="应收帐编号" prop="id"> </el-table-column>
      <el-table-column label="合同号" prop="contractid"> </el-table-column>
      <el-table-column label="合同名称" prop="contract.name"> </el-table-column>
      <el-table-column label="公司名称" prop="contract.quote.chance.clientCompany.clientcompanyname"> </el-table-column>
      <el-table-column label="公司编号" prop="contract.quote.chance.clientcompanyid"> </el-table-column>
      <el-table-column label="款项" prop="name"> </el-table-column>
      <el-table-column label="金额" prop="oughtpay"> </el-table-column>
      <el-table-column label="应付时间" prop="oughttime"> </el-table-column>
      <el-table-column label="已收款" prop="paypay"> </el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="{ row }">
          <el-button :icon="Edit" plain type="primary" @click="showDialog(row)">详细信息</el-button>
          <!-- <el-button :icon="Delete" circle plain type="danger" @click="del(row)"></el-button> -->
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="findmodel.pageNum" v-model:page-size="findmodel.pageSize"
      :page-sizes="[5, 10, 15, 20]" layout="jumper, total, sizes, prev, pager, next" background :total="total"
      @size-change="onSizeChange" @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end" />
  </el-card>
  <!-- 添加分类弹窗 -->
  <el-dialog v-model="dialogVisible" :title="title" width="45%">
    <el-form :model="pathmodel" ref="formref" :rules="rules" label-width="100px" style="padding-right: 30px">
      <div class="basic-info-border">
        <el-row>
        <el-col :span="11">
          <el-form-item label="合同号" prop="contractid">
            <el-select placeholder="请选择..." clearable style="width: 300px" @change="handleContractChange"
              v-model="pathmodel.contractid" disabled>
              <el-option v-for="c in contracts" :key="c.id" :label="c.id" :value="c.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="合同名称" prop="name">
            <el-input v-model="pathmodel.name" disabled style="width: 300px"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="公司名称" prop="clientcompanyname">
            <el-input v-model="pathmodel.clientcompanyname" disabled style="width: 300px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="合同总额" prop="allprices">
            <el-input v-model="pathmodel.allprices" disabled style="width: 300px"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="公司编号" prop="clientcompanyid">
            <el-input v-model="pathmodel.clientcompanyid" disabled style="width: 300px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="剩余金额" prop="resetprices">
            <el-input v-model="pathmodel.resetprices" disabled style="width: 300px"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      </div>
      <div class="basic-info-border2">
        <el-table :data="payphases" style="width: 100%">
          <el-table-column label="序号" type="index" width="60"></el-table-column>
          <el-table-column label="款项" prop="name"></el-table-column>
          <el-table-column label="计划收款时间" prop="oughttime"></el-table-column>
          <el-table-column label="实际收款时间" prop="paytime"></el-table-column>
          <el-table-column label="应收状态" width="80">
            <template v-slot="scope">
    {{ '应收' }}
  </template>
          </el-table-column>
          <el-table-column label="到款情况" prop="paypay"></el-table-column>
          <template #empty>
            <el-empty description="没有数据" />
          </template>
        </el-table>
      </div>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addpath"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>

</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
.basic-info-border {
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    right: -10px;
    position: relative;
    padding-top: 30px;
    /* 增加内边距，为标题留出更多空间 */
    margin-right: -10px;
    /* 调整外边距，使边框线不重合 */
}

.basic-info-border::before {
    content: "合同信息";
    position: absolute;
    top: -15px;
    /* 将标题移动到边框上方 */
    left: 10px;

    /* 根据需要调整左边距 */
    background-color: #409EFF;
    color: white;
    padding: 5px;
    border-radius: 3px;
    font-weight: bold;
}
.basic-info-border2 {
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    right: -10px;
    position: relative;
    padding-top: 30px;
    /* 增加内边距，为标题留出更多空间 */
    margin-right: -10px;
    /* 调整外边距，使边框线不重合 */
}

.basic-info-border2::before {
    content: "应收帐计划";
    position: absolute;
    top: -15px;
    /* 将标题移动到边框上方 */
    left: 10px;

    /* 根据需要调整左边距 */
    background-color: #409EFF;
    color: white;
    padding: 5px;
    border-radius: 3px;
    font-weight: bold;
}
</style>